<?php 
   require_once 'config.php';
   $action = getValue('action','str','POST');
   if($action == 'submit'){
      $username = getValue('username','str','POST');
      $password = getValue('password','str','POST');
      $mod_id   = isset($_POST['mod_id']) ? $_POST['mod_id'] : array();
      $last_id  = 0;
      if($username != '' && $password != ''){
         $db_sel = new db_query("SELECT 
                                       adt_id
                                    FROM
                                       administrator
                                    WHERE
                                       adt_username = '".$username."'
                                    ");
         $row = mysql_fetch_assoc($db_sel->result);
         if(isset($row['adt_id'])){
            echo 'Tên này đã tồn tại';
         }else{
            $sql_insert = "INSERT INTO 
                              administrator(adt_username,
                                            adt_password,
                                            adt_status)
                              VALUES('".$username."',
                                     '".md5($password)."',
                                     1)";
            $db_exe = new db_execute($sql_insert);
            $last_id = $db_exe->last_id;                                 
         }
         
      }else{
         echo 'Tên đăng nhập hoặc mật khẩu trống';
      }
      
      unset($db_exe);
      if($last_id > 0){
         if(count($mod_id) > 0){
            $sql_insert = "INSERT INTO 
                                 author(adt_id,
                                        mod_id)
                                 VALUES";
            $value = '';
            foreach($mod_id as $mid){
               $value .= '(' . $last_id . ',' . $mid . '),';
            }
            $value = substr($value,0,-1);
            $sql_insert .= $value;
            $db_exe = new db_execute($sql_insert);
            unset($db_exe);
         }
      }
      
   }
   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thêm mới <?=$moduol_name?></title>
   <?=$css_global?>
   <?=$js_global?>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $(document).on('click','.reload_tab' ,function(){
      window.location.reload();
    });
  });
  </script>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Thêm mới <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <div class="control-group">
               <label class="control-label" for="input01">Tên đăng nhập</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span3" id="username" name="username" title="Tên đăng nhập"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Mật khẩu</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span3" id="password" name="password" title="Mật khẩu"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Quyền</label>
               <div class="controls">
						<?
                     foreach($array_mod as $value){?>
                        <label>
                           <input type="checkbox" name="mod_id[]" id="mod_id" value="<?=$value['id']?>" />
                           <?=$value['fullname']?>
                        </label>
                     <?}
                  ?>
					</div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Thêm mới</button>
					<button type="reset"  class="btn btn-warning">Làm lại</button>
				</div>
         </fieldset>
      </form>
   </div> 
</div>